/**
 * 状态管理器 - 用户管理
 * 
 * @ version 1.0.0
 **/
import api from '../../api/index.js';
export default {
	namespaced: true,
	state: {
		id: null,
		isLogin: false,
		access_token:null,
		avatar:null,
		phonenumber:null,
		nickName:null,
		userName:null,
		people:{},
		dept:{},
		// nickName:null,
		
	},
	mutations: {
		setUserInfo(state, payload) {
			for (let i in payload) {
				for (let j in state) {
					if (i === j) {
						state[j] = payload[i];
					}
				}
			}
			uni.setStorageSync("xam:user", state);
		}
	},
	actions: {
		updateUserInfo({state,commit}) {
			api.getUserInfo().then(res => {
				console.log('res.data.user')
				console.log(res.user)
				commit('setUserInfo', res.user);
			})
		},
		async login({
			state,
			commit,
			dispatch,
			rootState
		}, data) {
			commit('setUserInfo', data);
			state.isLogin = true; // 登录状态强制 开启
			// 根据notice.vue 而知，fastadmin没有此三项默认字段，手动添加，想同步修改该这两处即可
			state.pushs = true; // 推送
			state.voice = true; // 提示音
			state.shock = true; // 震动
			state.access_token = data.access_token;
			// state.refresh_token = data.refresh_token;
			console.log(state);
			console.log("=============================");
			uni.setStorageSync("xam:user", state);
		},
		async logout({
			state,
			rootState
		}) {
			for (let j in state) {
				state[j] = ''
			}
			state.isLogin = false;
			// 从本地缓存中异步移除指定key
			uni.removeStorageSync('xam:user');
		}
	}
};
